﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using CivilRegistry.ControlledModel;
using CivilRegistry.Networking;
using CivilRegistry.Common;

namespace CivilRegistry.Services
{
    public class CivilServices : ICivilServices
    {
        public ServiceReport ActivateChildAccount()
        {
            ServiceReport report = new ServiceReport();
            report.StartTime = DateTime.Now;

            var nonActiveUsers = Person.GetNonActivatedUsers();
            var adminUser = Admin.FindById(1); //Root admin.

            foreach (var user in nonActiveUsers)
            {
                adminUser.ActivateUser(user);

                try
                {
                    EmailEngine emailEngine = new EmailEngine();
                    emailEngine.To.Add(user.EmailAddress);
                    emailEngine.Subject = Messages.EmailMessage.UserActivationSubject;
                    emailEngine.Body = Messages.EmailMessage.UserActivation
                        .Replace("<User>", user.BirthCertificates.FirstOrDefault().BirthReport.AssociatedParent.FirstName)
                        .Replace("<ChildUser>", user.FirstName)
                        .Replace("<UserName>", user.UserName)
                        .Replace("<Password>", user.Password)
                        .Replace("<Gender>", user.Gender == "M" ? "him" : "her");

                    emailEngine.Send();
                }
                catch (Exception ex)
                {
                    Logger.ErrorLogger.Log(ex);
                }

                try
                {
                    SMSEngine smsEngine = new SMSEngine();
                    smsEngine.PhoneNumber = user.PhoneNo;
                    smsEngine.Message = Messages.SMSMessage.UserActivation
                        .Replace("<ChildUser>", user.FirstName)
                        .Replace("<UserName>", user.UserName)
                        .Replace("<Password>", user.Password);
                    smsEngine.Send();
                }
                catch (Exception ex)
                {
                    Logger.ErrorLogger.Log(ex);
                }

                report.UserIds.Add(user.UserId);
                report.TotalCount++;
            }

            report.EndTime = DateTime.Now;
            return report;
        }

        public ServiceReport SendVoterCardReminder()
        {
            ServiceReport report = new ServiceReport();
            report.StartTime = DateTime.Now;

            var users = Person.GetAbove18UsersWithoutHavingVoterCard();

            foreach (var user in users)
            {
                try
                {
                    EmailEngine emailEngine = new EmailEngine();
                    emailEngine.To.Add(user.EmailAddress);
                    emailEngine.Subject = Messages.EmailMessage.VoterCardReminderSubject;
                    emailEngine.Body = Messages.EmailMessage.VoterCardReminder.Replace("<User>", user.FirstName);
                    emailEngine.Send();
                }
                catch (Exception ex)
                {
                    Logger.ErrorLogger.Log(ex);
                }

                try
                {
                    SMSEngine smsEngine = new SMSEngine();
                    smsEngine.PhoneNumber = user.PhoneNo;
                    smsEngine.Message = Messages.SMSMessage.VoterCardReminder;
                    smsEngine.Send();
                }
                catch (Exception ex)
                {
                    Logger.ErrorLogger.Log(ex);
                }
                report.UserIds.Add(user.UserId);
                report.TotalCount++;
            }

            report.EndTime = DateTime.Now;
            return report;
        }

    }
}
